import Search from '@/pages/Search'
import Login from '@/pages/Login'
import Register from '@/pages/Register'
import Detail from '@/pages/Detail'
import AddCartSuccess from '@/pages/AddCartSuccess'
import ShopCart from '@/pages/ShopCart'
import Trade from '@/pages/Trade'
import Pay from '@/pages/Pay'
import PaySuccess from '@/pages/PaySuccess'
import Center from '@/pages/Center'
import MyOrder from '@/pages/Center/myOrder'
import GroupOrder from '@/pages/Center/groupOrder'

export default [
  {
    path: "/center",
    component: Center,
    name: 'center',
    meta: { show: true },
    children: [
      {
        path: 'myorder',
        component: MyOrder
      },
      {
        path: 'groupOrder',
        component: GroupOrder
      },
      {
        path: '',
        component: MyOrder
      }
    ]
  },
  {
    path: "/paysuccess",
    component: PaySuccess,
    name: 'paysuccess',
    meta: { show: true },
    /* 只有从支付界面, 才能跳转到支付成功的界面 */
    beforeEnter (to, from, next) {
      if (from.path === '/pay') {
        next()
      } else {
        next('/pay')
      }
    }
  },
  {
    path: "/pay",
    component: Pay,
    name: 'pay',
    meta: { show: true },
    /* 只能从交易界面, 才能跳转到支付界面 */
    beforeEnter (to, from, next) {
      if (from.path === '/trade') {
        next()
      } else {
        next('/trade')
      }
    }
  },
  {
    path: "/trade",
    component: Trade,
    name: 'trade',
    meta: { show: true },
    /* 只能从购物车界面, 才能跳转到交易界面 */
    beforeEnter (to, from, next) {
      if (from.path === '/shopcart') {
        next()
      } else {
        next('/shopcart')
      }
    }
  },
  {
    path: "/shopcart",
    component: ShopCart,
    name: 'shopcart',
    meta: { show: true }
  },
  {
    path: "/addcartsuccess",
    component: AddCartSuccess,
    name: 'addcartsuccess',
    meta: { show: true },
    beforeEnter (to, from, next) {
      // 得到当前路由信息对象
      // const route = router.currentRoute  // route就是from

      // 得到要跳转到目路由的query参数
      const skuNum = to.query.skuNum
      // 读取保存的数据
      const skuInfo = JSON.parse(window.sessionStorage.getItem('SKUINFO'))
      // 只有都存在, 才放行
      if (skuNum && skuInfo) {
        next()
      } else { // 在组件对象创建前强制跳转到首页
        next('/')
      }
    }
  },
  {
    path: "/home",
    component: () => import('@/pages/Home'),
    meta: { show: true }
  },
  {
    path: "/search/:keyword?",
    component: Search,
    meta: { show: true },
    name: 'search'
  },
  {
    path: "/login",
    component: Login,
    meta: { show: false }
  },
  {
    path: "/detail/:skuId",
    component: Detail,
    meta: { show: true }
  },
  {
    path: "/register",
    component: Register,
    meta: { show: false }
  },
  {
    path: '*',
    redirect: "/home"
  }
]
